import {Module, NestModule, MiddlewareConsumer, RequestMethod} from '@nestjs/common';
import {AdminModule} from './module/admin/admin.module';
import {TypeOrmModule} from "@nestjs/typeorm";
import {ToolsService} from './service/tools/tools.service';
import {AdminauthMiddleware} from "./middleware/adminauth/adminauth.middleware";
import {InitMiddleware} from "./middleware/init/init.middleware";

@Module({
    imports: [
        AdminModule,
        // 配置数据库链接
        TypeOrmModule.forRoot({
            type: 'mysql',
            host: '127.0.0.1',
            port: 3306,
            username: 'root',
            password: '123456',
            database: 'nest_example',
            entities: [__dirname + '/**/*.entity{.ts,.js}'],
        }),
    ],
    providers: [ToolsService],
})
export class AppModule implements NestModule {
    configure(consumer: MiddlewareConsumer) {
        consumer
            .apply(InitMiddleware)
            .forRoutes({
                path: 'admin/*',
                method: RequestMethod.ALL,
            })
            .apply(AdminauthMiddleware)
            .forRoutes('*');
    }
}

